<?php

$currentUrl = $_SERVER['REQUEST_URI'];
$split = explode('?', $currentUrl);
$currentUrl = _decrypt($split[1], "8659471");
$splitData = explode('|', $currentUrl);

$appname=$splitData[0];//$_GET['appname'];
$appid=$splitData[1];//$_GET['appid'];
$ipaurl=$splitData[2];//$_GET['ipaurl'];

// 创建一个新的DOMDocument对象
$dom = new DOMDocument("1.0", "UTF-8");
$dom->formatOutput = true;

// 创建根节点<plist>
$plist = $dom->createElement('plist');
$plist->setAttribute('version', '1.0');
$dom->appendChild($plist);

// 创建<dict>节点
$dict = $dom->createElement('dict');
$plist->appendChild($dict);

// 创建<key>items</key>和<array>节点
$keyItems = $dom->createElement('key', 'items');
$dict->appendChild($keyItems);
$arrayItems = $dom->createElement('array');
$dict->appendChild($arrayItems);

// 在<array>下创建第一个<dict>
$dictItem = $dom->createElement('dict');
$arrayItems->appendChild($dictItem);

// 添加<key>assets</key>和<array>节点
$keyAssets = $dom->createElement('key', 'assets');
$dictItem->appendChild($keyAssets);
$arrayAssets = $dom->createElement('array');
$dictItem->appendChild($arrayAssets);

// 添加software-package到<array>
$dictAssetSoftware = $dom->createElement('dict');
$arrayAssets->appendChild($dictAssetSoftware);
$keyKindSoftware = $dom->createElement('key', 'kind');
$dictAssetSoftware->appendChild($keyKindSoftware);
$textKindSoftware = $dom->createElement('string', 'software-package');
$dictAssetSoftware->appendChild($textKindSoftware);
$keyUrlSoftware = $dom->createElement('key', 'url');
$dictAssetSoftware->appendChild($keyUrlSoftware);
$textUrlSoftware = $dom->createElement('string', $ipaurl);
$dictAssetSoftware->appendChild($textUrlSoftware);



// 添加<key>metadata</key>和<dict>节点
$keyMetadata = $dom->createElement('key', 'metadata');
$dictItem->appendChild($keyMetadata);
$dictMetadata = $dom->createElement('dict');
$dictItem->appendChild($dictMetadata);

// 在<dict>metadata下添加子节点并确保每个value都用<string>包裹
$metadataFields = [
    'bundle-identifier' => $appid,
    'bundle-version' => '1.0',
    'kind' => 'software',
    'subtitle' => 'Okrain Inc.',
    'title' => $appname
];

foreach ($metadataFields as $key => $value) {
    $keyNode = $dom->createElement('key', $key);
    $dictMetadata->appendChild($keyNode);
    $stringValue = $dom->createElement('string', $value);
    $dictMetadata->appendChild($stringValue);
}

// 构建完整的XML字符串，包括DOCTYPE
$xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".
             "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n".
             "{$dom->saveXML($plist)}";

// 输出最终的XML
echo $xmlString;

function _decrypt($data, $key) {
	$data = str_replace(array('yqq','okrain','haha'), array('=','+','/'),$data);
	$key = md5($key);
	$x = 0;
	$data = base64_decode($data);
	$expire = substr($data, 0, 10);
	$data = substr($data, 10);
	if($expire > 0 && $expire < time()) {
		return null;
	}
	$len = strlen($data);
	$l = strlen($key);
	$char = $str = '';
	for ($i = 0; $i < $len; $i++) {
		if ($x == $l) $x = 0;
		$char .= substr($key, $x, 1);
		$x++;
	}
	for ($i = 0; $i < $len; $i++) {
		if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
			$str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
		} else {
			$str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
		}
	}
	return base64_decode($str);
}
?>